var integral_catId = 0, integral_index = -1, page = 1;

Page({
    data: {
      goods_list: [],
      switchList: [
        {
          title: '兑换抽奖'
        },
        {
          title: '限时兑换'
        },
        {
          title: '人人砍价'
        },
      ],
      classify_list: [
        {
          id: 1,
          name: '饰品'
        },
        {
          id: 1,
          name: '家装'
        },
        {
          id: 1,
          name: '服饰'
        },
        {
          id: 1,
          name: '家具'
        },
        {
          id: 1,
          name: '食品'
        },
        {
          id: 1,
          name: '数码'
        },
        {
          id: 1,
          name: '生活用品'
        },
      ],
      classify_active: 1,
      tabIndex: 0,
      //轮播属性
      current: 0,
      exchangeNumber: 0,
      keywords: ''
    },
  goSearchGoods(e) {
    this.setData({
      keywords: e.detail.value
    })
  },
  searchHandle(){
    this.setData({
      page: 1,
      goods_list: []
    })
    this.getGoodsList(0)
  },
  classifyHandle(e) {
    let index = e.currentTarget.dataset.index
    this.setData({
      classify_active: index
    })
  },
  switchHandle(e){
    let tabIndex = e.currentTarget.dataset.index

    this.setData({
      tabIndex
    })
  },
  getRollScreen(e){
    getApp().request({
      url: getApp().api.integral.roll_screen,
      data: {},
      success: (res) => {
        this.setData({
          exchangeNumber: res.list
        })
      },
      complete: function (t) {
        getApp().core.hideLoading();
      }
    });
    this.data.getRollScreenTimer = setTimeout(()=>{
      this.getRollScreen()
    }, 30000)
  },
    onLoad: function(t) {
        getApp().page.onLoad(this, t),
         integral_catId = 0, integral_index = -1;
        page = 1, this.getGoodsList(integral_catId);
    },
    onReady: function(t) {
        getApp().page.onReady(this);
    },
    onShow: function(t) {
        getApp().page.onShow(this);
        var a = this;
        getApp().request({
            url: getApp().api.integral.index,
            data: {},
            success: function(t) {
                if (0 == t.code && (t.data.today && a.setData({
                    register_day: 1
                }), a.setData({
                    banner_list: t.data.banner_list,
                    coupon_list: t.data.coupon_list,
                    integral: t.data.user.integral,
                    catList: t.data.cat_list
                }), -1 != integral_index)) {
                    var e = [];
                    e.index = integral_index, e.catId = integral_catId, a.catGoods({
                        currentTarget: {
                            dataset: e
                        }
                    });
                }
            },
            complete: function(t) {
                getApp().core.hideLoading();
            }
        });
      this.getRollScreen()
    },
    exchangeCoupon: function(t) {
        var a = this, n = a.data.coupon_list, e = t.currentTarget.dataset.index, o = n[e], i = a.data.integral;
        if (parseInt(o.integral) > parseInt(i)) a.setData({
            showModel: !0,
            content: "当前爱心不足",
            status: 1
        }); else {
            if (0 < parseFloat(o.price)) var s = "需要" + o.integral + "爱心+￥" + parseFloat(o.price); else s = "需要" + o.integral + "爱心";
            if (parseInt(o.total_num) <= 0) return void a.setData({
                showModel: !0,
                content: "已领完,来晚一步",
                status: 1
            });
            if (parseInt(o.num) >= parseInt(o.user_num)) return o.type = 1, void a.setData({
                showModel: !0,
                content: "兑换次数已达上限",
                status: 1,
                coupon_list: n
            });
            getApp().core.showModal({
                title: "确认兑换",
                content: s,
                success: function(t) {
                    t.confirm && (0 < parseFloat(o.price) ? (getApp().core.showLoading({
                        title: "提交中"
                    }), getApp().request({
                        url: getApp().integral.exchange_coupon,
                        data: {
                            id: o.id,
                            type: 2
                        },
                        success: function(e) {
                            0 == e.code && getApp().core.requestPayment({
                                _res: e,
                                timeStamp: e.data.timeStamp,
                                nonceStr: e.data.nonceStr,
                                package: e.data.package,
                                signType: e.data.signType,
                                paySign: e.data.paySign,
                                success: function(e) {
                                  wx.requestSubscribeMessage({
                                      tmplIds: ['_XGsVLLisG8_LTbDnBk_4yIr2GbwvkNi8aSsKiUN_80'],
                                      success (res) { 
                                          console.log('订阅消息',res)
                                      }
                                    })
                              },
                                complete: function(t) {
                                    "requestPayment:fail" != t.errMsg && "requestPayment:fail cancel" != t.errMsg ? "requestPayment:ok" == t.errMsg && (o.num = parseInt(o.num), 
                                    o.num += 1, o.total_num = parseInt(o.total_num), o.total_num -= 1, i = parseInt(i), 
                                    i -= parseInt(o.integral), a.setData({
                                        showModel: !0,
                                        status: 4,
                                        content: e.msg,
                                        coupon_list: n,
                                        integral: i
                                    })) : getApp().core.showModal({
                                        title: "提示",
                                        content: "订单尚未支付",
                                        showCancel: !1,
                                        confirmText: "确认"
                                    });
                                }
                            });
                        },
                        complete: function() {
                            getApp().core.hideLoading();
                        }
                    })) : (getApp().core.showLoading({
                        title: "提交中"
                    }), getApp().request({
                        url: getApp().api.integral.exchange_coupon,
                        data: {
                            id: o.id,
                            type: 1
                        },
                        success: function(t) {
                            0 == t.code && (o.num = parseInt(o.num), o.num += 1, o.total_num = parseInt(o.total_num), 
                            o.total_num -= 1, i = parseInt(i), i -= parseInt(o.integral), a.setData({
                                showModel: !0,
                                status: 4,
                                content: t.msg,
                                coupon_list: n,
                                integral: i
                            }));
                        },
                        complete: function() {
                            getApp().core.hideLoading();
                        }
                    })));
                }
            });
        }
    },
    hideModal: function() {
        this.setData({
            showModel: !1
        });
    },
    couponInfo: function(t) {
        var e = t.currentTarget.dataset;
        getApp().core.navigateTo({
            url: "/pages/integral-mall/coupon-info/index?coupon_id=" + e.id
        });
    },
    goodsAll: function() {
        var t = this.data.goods_list, e = [];
        for (var a in t) {
            var n = t[a].goods;
            for (var o in t[a].cat_checked = !1, n) e.push(n[o]);
        }
        this.setData({
            index_goods: e,
            cat_checked: !0,
            goods_list: t
        });
    },
    catGoods: function(t) {
        var e = t.currentTarget.dataset, a = this, n = a.data.catList;
        integral_catId = e.catId, integral_index = e.index;
        var o = e.index;
        if (-1 === o) {
            var i = !0;
            for (var s in n) n[s].cat_checked = !1;
        }
        if (0 <= o) for (var s in n) n[s].id == n[o].id ? i = !(n[s].cat_checked = !0) : n[s].cat_checked = !1;
        a.setData({
            cat_checked: i,
            catList: n,
            goods_list: []
        }), page = 1, a.getGoodsList(integral_catId);
    },
    getGoodsList: function(t) {
        var a = this;
        -1 === integral_index && a.setData({
            cat_checked: !0
        }), getApp().core.showLoading({
            title: "加载中"
        }), getApp().request({
            url: getApp().api.integral.goods_list,
            data: {
                page: page,
                cat_id: t,
                keywords: this.data.keywords
            },
            success: function(t) {
                if (0 === t.code) {
                    var e = a.data.goods_list;
                    0 < t.data.list.length && (0 < e.length && (e = e.concat(t.data.list)), 0 === e.length && (e = t.data.list), 
                    page += 1), 0 === t.data.list.length && getApp().core.showToast({
                        title: "没有更多啦",
                        icon: "none"
                    }), a.setData({
                        goods_list: e
                    });
                }
            },
            complete: function() {
                getApp().core.hideLoading();
            }
        });
    },
    goodsInfo: function(e) {
      let that = this;
      var r = this.data.integral, s = e.currentTarget.dataset.integral;
      let goods_index = e.currentTarget.dataset.index
      if (parseInt(r) < parseInt(s)) return getApp().core.showToast({
        title: "爱心不足!",
        image: "/images/icon-warning.png"
      })
      console.log(that.data.goods_list,goods_index)
      let a = JSON.parse(that.data.goods_list[goods_index].attr)[0].attr_list[0]
      let attr = [{
        attr_group_id: 0,
        attr_group_name: '规格',
        attr_id: a.attr_id,
        attr_name: a.attr_name
      }]
      let goods_id = e.currentTarget.dataset.goodsId;
      let attr_price
      let attr_integral
      let type = e.currentTarget.dataset.type
      if (type == 'order'){
        getApp().core.navigateTo({
          url: "/pages/integral-mall/order-submit/index?goods_info=" + JSON.stringify({
            goods_id,
            attr,
            attr_price,
            attr_integral
          })
        })
      }
      else if (type == 'goods'){
        getApp().core.navigateTo({
          url: "/pages/integral-mall/goods-info/index?goods_id=" + goods_id + "&integral=" + this.data.integral
        });
      }
      
    },
  ljgm: function (e) {
    let that = this;
    let goods_index = e.currentTarget.dataset.index
    //用户确认兑换
    wx.showModal({
      title: '一爱心抽奖',
      content: '需要消耗' + e.currentTarget.dataset.integral + '个爱心抽奖吗？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#000000',
      success: (sres)=> {
        if (sres.confirm) {
          let data = {
            goods_info: JSON.stringify({
              goods_id: that.data.goods_list[goods_index].id,
              attr: [{ "attr_group_id": 1, "attr_group_name": "规格", "attr_id": 1, "attr_name": "默认" }]
            })
          }
          // that.data.goods_list[goods_index].attr
          // { "goods_id": "3", "attr": [{ "attr_group_id": 1, "attr_group_name": "规格", "attr_id": 1, "attr_name": "默认" }] }
          getApp().request({
            url: getApp().api.integral.luck_draw,
            data: data,
            method: 'post',
            success: (res) => {
              if (res.status == 0) {
                wx.showModal({
                  title: '提示',
                  content: res.msg,
                  showCancel: false
                })
              }
              else if (res.status == 1) {
                wx.showModal({
                  title: '提示',
                  content: '恭喜！中大奖了 点击确定填写收货地址',
                  showCancel: false,
                  success: re => {
                    // {"goods_id":"3","attr":[{"attr_group_id":1,"attr_group_name":"规格","attr_id":1,"attr_name":"默认"}]}
                    let a = JSON.parse(that.data.goods_list[goods_index].attr)[0].attr_list[0]
                    let attr = [{
                      attr_group_id: 0,
                      attr_group_name: '规格',
                      attr_id: a.attr_id,
                      attr_name: a.attr_name
                    }]
                    let goods_id = e.currentTarget.dataset.goodsId;
                    let attr_price
                    let attr_integral
                    wx.navigateTo({
                      url: "/pages/integral-mall/order-submit/index?log_id=" + res.log_id + "&goods_info=" + JSON.stringify({
                        goods_id,
                        attr,
                        attr_price,
                        attr_integral
                      })
                    })
                  }
                })
              }
              else if (res.status == 2) {
                wx.showModal({
                  title: '提示',
                  content: res.msg,
                  showCancel: false,
                  success: res => {

                  }
                })
              }
              else {
                wx.showModal({
                  title: '提示',
                  content: res.msg,
                  showCancel: false,
                  success: res => {

                  }
                })
              }
            },
            complete: function (t) {
              getApp().core.hideLoading();
            }
          });
        }
      }
    })
    
  },
    onHide: function(t) {
        getApp().page.onHide(this);
        clearTimeout(this.data.getRollScreenTimer)
    },
    onUnload: function(t) {
        getApp().page.onUnload(this);
        clearTimeout(this.data.getRollScreenTimer)
    },
    onPullDownRefresh: function(t) {
        getApp().page.onPullDownRefresh(this);
    },
    onShareAppMessage: function() {
        getApp().page.onShareAppMessage(this);
        var t = getApp().getUser(), e = "", a = getApp().core.getStorageSync(getApp().const.WX_BAR_TITLE);
        for (var n in a) if (a[n] && "pages/tuibao-integral-mall/index/index" === a[n].url) {
            e = a[n].title;
            break;
        }
        return {
            path: "/pages/tuibao-integral-mall/index/index?user_id=" + t.id,
            title: e || "爱心商城"
        };
    },
    onReachBottom: function(t) {
        getApp().page.onReachBottom(this);
        this.getGoodsList(integral_catId);
    },
    shuoming: function() {
        getApp().core.navigateTo({
            url: "/pages/integral-mall/shuoming/index"
        });
    },
    detail: function() {
        getApp().core.navigateTo({
            url: "/pages/integral-mall/detail/index"
        });
    },
    exchange: function() {
        getApp().core.navigateTo({
            url: "/pages/integral-mall/exchange/index"
        });
    },
    register: function() {
        getApp().core.navigateTo({
            url: "/pages/integral-mall/register/index"
        });
    }
});